package com.wanmait.translate.administration.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.translate.administration.service.AdministrationTicketService;
import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.AdministrationTicket;
import com.wanmait.translate.pojo.City;
import com.wanmait.translate.pojo.Province;
import com.wanmait.translate.utils.CommonMessage;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class LogisticsTicketController {
    @Resource
    private AdministrationTicketService administrationTicketService;

    @GetMapping("/logistics/logisticsList")
    public ModelAndView logisticsList(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        AdministrationTicket administrationTicket = administrationTicketService.selectOne(id);
        modelAndView.addObject("administrationTicket",administrationTicket);
        List<Province> provinces = administrationTicketService.ListAllProvince();
        modelAndView.addObject("provinces",provinces);
        return modelAndView;
    }
    @PostMapping("/logistics/insertLogisticsList")
    public void insert(AdministrationTicket administrationTicket){

        administrationTicketService.insert(administrationTicket);
    }

    @PostMapping("logistics/updateLogisticsList")
    public void update(AdministrationTicket administrationTicket){
        administrationTicketService.update(administrationTicket);
    }

    @RequestMapping("/logistics/bookings")
    @ResponseBody
    public ResultData<AdministrationTicket> travelLists(Integer page, Integer limit, String name, Integer booking){
        PageInfo<AdministrationTicket> ticketPageInfo = null;
        if (name != null && !"".equals(name)) {
            ticketPageInfo = administrationTicketService.ListAllByName(page,limit,name);
        }else if (booking!=null) {
            if(booking==0){
                ticketPageInfo = administrationTicketService.ListAllByNotBooking(page,limit);
            }else {
                ticketPageInfo = administrationTicketService.ListAllByBooking(page,limit);
            }
        } else {
            ticketPageInfo = administrationTicketService.listAllByPage(page, limit);
        }
        ResultData<AdministrationTicket >resultData = ResultData.successsWithData(0, ticketPageInfo);
        return resultData;
    }

   @RequestMapping("/logistics/booking")
    public ResultData<AdministrationTicket> travelList(){
       ResultData<AdministrationTicket> rd = null;
       try {
           rd = ResultData.SuccesssWithData(administrationTicketService.selectList());
       } catch (Exception e) {
           e.printStackTrace();
           rd = ResultData.fail(CommonMessage.ERR_MSG);
       }
       return rd;
    }
    @GetMapping("/logistics/insertBooking")
    public void insertBookingList(AdministrationTicket administrationTicket){
        administrationTicketService.insert(administrationTicket);
    }

    @RequestMapping("/logistics/province")
    @ResponseBody
        public List<City> allProvince(Integer provinceId){
        return administrationTicketService.ListCitiesById(provinceId);
    }

    //时间格式格式化
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }
}
